package com.liuzy.dome;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.liuzy.entity.Card;
import com.liuzy.entity.Emp;
import com.liuzy.entity.Order;
import com.liuzy.entity.User;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class TestPoi {
    /**
     * 模拟数据
     * @return
     */
    public List<User> getUsers(){
        ArrayList<User> users = new ArrayList<User>();
        for (int i = 0; i < 5; i++) {
            User user = new User();
            user.setId(i);
            user.setName("刘哲源"+i);
            user.setAge(10+i);
            user.setBir(new Date());
            if (i%2==0){
                user.setStatus("1");
                user.setHabbys(Arrays.asList("吃饭","睡觉","打豆豆"));
            }else {
                user.setStatus("0");
                user.setHabbys(Arrays.asList("打球","学习","看片"));
            }
            //身份信息
            user.setCard(new Card("88892282828282222","陕西西安"));
//            //订单信息
//            ArrayList<Order> orders = new ArrayList<>();
//            orders.add(new Order("12","超短裙"));
//            orders.add(new Order("13","jk"));
//            orders.add(new Order("14","汉服"));
//            orders.add(new Order("15","制服"));
//            user.setOrders(orders);
           // user.setPhoto("C:\\Users\\刘哲源\\Desktop\\微信图片_20210603230150.jpg");
            users.add(user);
        }
        return users;
    }
    /**
     * 导出excel
     */
    @Test
    public void testExport() throws IOException {
        List<User> users = getUsers();
        //导出
        /**
         * 参数1：ExportParams 导出配置对象 参数2：导出的类型 参数3：导出的集合
         */
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("用户信息列表", "用户信息"), User.class, users);
        //导出excel 通过流写入指定位置
        FileOutputStream outputStream = new FileOutputStream("C:\\Users\\刘哲源\\Desktop\\poi\\用户.xlsx");
        workbook.write(outputStream);
        outputStream.close();
        workbook.close();
    }
    @Test
    public void testBigExport() throws Exception {
        List<User> users = getUsers();
        //导出
        /**
         * 参数1：ExportParams 导出配置对象 参数2：导出的类型 参数3：导出的集合
         */
        Workbook workbook = ExcelExportUtil.exportBigExcel(new ExportParams("用户信息列表", "用户信息"), User.class, users);
        //导出excel 通过流写入指定位置
        FileOutputStream outputStream = new FileOutputStream("C:\\Users\\刘哲源\\Desktop\\poi\\用户.xlsx");
        workbook.write(outputStream);
        outputStream.close();
        workbook.close();
    }
    /**
     * 导入
     */
    @Test
    public void testImportExcel() throws Exception {
        //参数1:导入的文件流 参数2:导入的类型 参数3:导入的配置对象
        ImportParams params = new ImportParams();
        params.setTitleRows(1);//标题列占几行
        params.setHeadRows(2);//heard列
        List<Emp> emps = ExcelImportUtil.importExcel(new FileInputStream("C:\\Users\\刘哲源\\Desktop\\poi\\用户.xlsx"), Emp.class, params);
        emps.forEach(System.out::println);
    }
}
